package com.test.controller;



import com.test.pojo.User;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.beetl.sql.core.engine.PageQuery;
import org.beetl.sql.core.kit.StringKit;
import org.omg.CORBA.CharSeqHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MyController {

    //springboot自带日志 slf4j+logback
    Logger logger= LoggerFactory.getLogger(MyController.class);

    @Autowired
    public SQLManager sqlmanager;



    @RequestMapping(value = "/test1/{id}")
    @ResponseBody
    public User getBody1(@PathVariable("id") int id){
        User user =null;

        //单个查询找不到会抛错
        try {
            user = sqlmanager.unique(User.class,id);
        } catch (Exception e) {

            logger.info("未查询到结果");
        }


        logger.info("查找的结果是---"+user);
        return user;
    }



    //根据表来生成 sql语句 pojo类等
    @RequestMapping(value = "/test2")
    @ResponseBody
    public void getBody2(){

        try {
            sqlmanager.genPojoCodeToConsole("user");
            sqlmanager.genSQLTemplateToConsole("user");
        } catch (Exception e) {
            logger.info("未查询到表");
        }
    }


    //public int allCount(Class<?> clazz) 总数
    @RequestMapping(value = "/test3")
    @ResponseBody
    public long getBody3(){

        long l = sqlmanager.allCount(User.class);
        logger.info("共有"+l+"条结果");
        return l;

    }


    // 模板 查询
    @RequestMapping(value = "/test4")
    @ResponseBody
    public User getBody4(){
                User template = new User();
                template.setName("张三");

                User user = sqlmanager.templateOne(template);

                return user;

    }

    //范围查询
    // public List select(String sqlId, Class clazz, Map<String, Object> paras, int start, int size)， 查询指定范围
    //public List select(String sqlId, Class clazz, Object paras, int start, int size) ，查询指定范围

    @RequestMapping(value = "/test5")
    @ResponseBody
    public  List<User> getBody5(){


        Map<String, Object> map=new HashMap<>();
        map.put("name","xian");


        //查询的结果取  前十个       注意 limit 还是  0--10   start自动减一
        List<User> list = sqlmanager.select("user.select2", User.class, map, 1, 10);

        return list;

    }


    @RequestMapping(value = "/test6")
    @ResponseBody
    public  List<User> getBody6(){


        //从第一页开始查询,无参数  默认  20个一页
        PageQuery query = getPageQuery(1, 2);


        sqlmanager.pageQuery("user.queryNewUser", User.class,query);
        System.out.println(query.getTotalPage());
        System.out.println(query.getTotalRow());
        System.out.println(query.getPageNumber());

        List<User> list = query.getList();
        return list;

    }

    //直接使用sql模板
    @RequestMapping(value = "/test7")
    @ResponseBody
    public  List<User> getBody7(){


        //注意会自动执行 select count(1) from user  这条语句  query会拿到总数

        String  jdbcSql = "  select *from user order by id";
        PageQuery query = new PageQuery(1,20);

        query = sqlmanager.execute(new SQLReady(jdbcSql), User.class, query);

        System.out.println(query.getTotalRow());

        List<User> list = query.getList();
        return list;

    }






    //***********************************************************************************

    @RequestMapping(value = "/view1")

    public String getView1(){

        return "view/1";
    }


    @RequestMapping(value = "/view2")

    public String getView2(){

        return "view/2";
    }

    //*********************************************************************************** 以下用于工具方法

    // 封装的方法 重载  用于分页查询

    public PageQuery getPageQuery(int page, int perpage) {

        return getPageQuery(page,perpage,null);
    }


    public PageQuery getPageQuery() {

        return getPageQuery(0,0,null);
    }


    public PageQuery getPageQuery(int page, int perpage, Map<String,Object> params) {


        PageQuery query=new PageQuery();

        query.setPageNumber(page == 0?1:page);
        query.setPageSize(perpage==0?10:perpage);

        query.setParas(params);

        return query;
    }

}
